package org.rency.crawler.fetch.repository.impl;

import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.rency.crawler.fetch.repository.FetchedQueueRepository;
import org.rency.crawler.fetch.repository.MetaResourceRepository;
import org.rency.crawler.integration.cache.FetchedCacheClient;
import org.springframework.stereotype.Repository;

/**
 * 名称:FetchedQueueRepositoryImpl
 * 描述:${TODO}
 *
 * @author:rency
 * @version:V1.0.0
 * @JDK 1.8
 * @since 2017/11/22 21:13
 */
@Repository
public class FetchedQueueRepositoryImpl implements FetchedQueueRepository {

  @Resource
  private MetaResourceRepository metaResourceRepository;

  @Resource
  private FetchedCacheClient     fetchedCacheClient;

  @Override
  public boolean exists(String fetchNo) {
    String fetchUri = fetchedCacheClient.get(fetchNo);
    return StringUtils.isNotEmpty(fetchUri);
  }

  @Override
  public boolean save(String fetchNo,String fetchUri) {
    fetchedCacheClient.set(fetchNo,fetchUri,metaResourceRepository.getFetchedQueueExpireTime());
    return true;
  }

  @Override
  public boolean delete(String fetchNo) {
    fetchedCacheClient.remove(fetchNo);
    return true;
  }

  @Override
  public int deleteAll() {
    return fetchedCacheClient.removeAll();
  }

}
